package jinyilw.swing.graphics;

import java.awt.Graphics2D;
import java.awt.RenderingHints;

public class RenderingConfig
{
	public static final boolean MAC_USE_QUARTZ = Boolean
			.getBoolean("apple.awt.graphics.UseQuartz");
	public static final Object STROKE_HINT = MAC_USE_QUARTZ
			? RenderingHints.VALUE_STROKE_PURE
			: RenderingHints.VALUE_STROKE_NORMALIZE;
	private Object antialiasHint, strokeHint, textHint;
	public RenderingConfig()
	{
	}
	public RenderingConfig(Object antialiasHint, Object strokeHint)
	{
		this.antialiasHint = antialiasHint;
		this.strokeHint = strokeHint;
	}

	public static RenderingConfig enableAAPainting(Graphics2D g2d)
	{
		RenderingConfig renderingConfig = new RenderingConfig();
		renderingConfig.setAAPainting(g2d);
		return renderingConfig;
	}

	public static void resetAAPainting(Graphics2D g2d,
			RenderingConfig renderingConfig)
	{
		renderingConfig.resetAAPainting(g2d);
	}

	public void setAAPainting(Graphics2D g2d)
	{
		antialiasHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
		strokeHint = g2d.getRenderingHint(RenderingHints.KEY_STROKE_CONTROL);
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, STROKE_HINT);
	}

	public void resetAAPainting(Graphics2D g2d)
	{
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, antialiasHint);
		g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, strokeHint);
	}

	public void setTextAADrawing(Graphics2D g2d)
	{
		textHint = g2d.getRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING);
		g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
				RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
	}

	public void resetTextAADrawing(Graphics2D g2d)
	{
		g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, textHint);
	}

	public void setAllAAPainting(Graphics2D g2d)
	{
		antialiasHint = g2d.getRenderingHint(RenderingHints.KEY_ANTIALIASING);
		strokeHint = g2d.getRenderingHint(RenderingHints.KEY_STROKE_CONTROL);
		textHint = g2d.getRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING);
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
				RenderingHints.VALUE_ANTIALIAS_ON);
		g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, STROKE_HINT);
		g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,
				RenderingHints.VALUE_TEXT_ANTIALIAS_ON);
	}

	public void resetAllAAPainting(Graphics2D g2d)
	{
		g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, antialiasHint);
		g2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, strokeHint);
		g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, textHint);
	}
}
